package com.hrm.service.impl;

import cn.hutool.core.date.DateUtil;
import com.hrm.dao.SysEventMapper;
import com.hrm.entity.Employee;
import com.hrm.entity.SysEvent;
import com.hrm.service.SystemService;

import com.hrm.utils.SysEventDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Auther: zy
 * @Date: 2020/5/24 20:31
 * @Description:
 */
@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    SysEventMapper sysEventMapper;

    @Override
    public boolean backupDB(HttpSession session) {
        Employee employeeSession = (Employee) session.getAttribute("user");
        String filePath="E:\\backupDB\\";
        /*String fileName="批量上传模板.xlsx";*/
        String dbName="ssm_hrms";
        String description = SysEventDescription.addSysEventDescription(employeeSession.getId(),employeeSession.getName(),"备份系统");
        SysEvent sysEvent = SysEventDescription.addSysEvent(DateUtil.date(),description);
        sysEventMapper.insert(sysEvent);
        try {
            Process process = Runtime.getRuntime().exec(
                    "cmd  /c  mysqldump -u root -p123456 " + dbName + " > "
                            + filePath + "/" + dbName
                            + ".sql");
            //备份的数据库名字为teacher，数据库连接和密码均为root
            System.out.println("数据库备份成功!");
            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean restoreDB(HttpSession session) {
        Employee employeeSession = (Employee) session.getAttribute("user");
        String filePath="E:\\backupDB\\";
        String dbName="ssm_hrms";
        try {
            Process process = Runtime.getRuntime().exec(
                    "cmd  /c  mysql -u root -p123456 " + dbName + " < "
                            + filePath + "/" + dbName
                            + ".sql");
            //备份的数据库名字为teacher，数据库连接和密码均为root
            System.out.println("数据库还原成功!");
            String description = SysEventDescription.addSysEventDescription(employeeSession.getId(),employeeSession.getName(),"还原数据库");
            SysEvent sysEvent = SysEventDescription.addSysEvent(DateUtil.date(),description);
            sysEventMapper.insert(sysEvent);
            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
}
